//
//  DownloadCarObj.m
//  Honda
//
//  Created by Xiaolong Zhang on 13-5-14.
//  Copyright (c) 2013年 HeninHoo. All rights reserved.
//

#import "DownloadCarObj.h"
#import "ConfigCodeHelper.h"
@implementation DownloadCarObj
@synthesize uuid                =   _uuid;
@synthesize tempCarPath         =   _tempCarPath;
@synthesize tempFilePath        =   _tempFilePath;
@synthesize tempInfoPath        =   _tempInfoPath;
@synthesize saveFilePath        =   _saveFilePath;
@synthesize saveAutosPath       =   _saveAutosPath;
@synthesize state               =   _state;
@synthesize progress            =   _progress;
@synthesize eTag                =   _eTag;


- (id)initWithDownloadObjectType:(DownloadObjectType)downloadObjType
{
    self = [super init];
    if (self) {
        
        CFUUIDRef uuidObj = CFUUIDCreate(nil);
        _uuid = (NSString*)CFUUIDCreateString(nil, uuidObj);
        CFRelease(uuidObj);
        
        _objType = downloadObjType;
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        if (IsCarInfo == _objType) {
            _saveAutosPath = [[NSString stringWithFormat:@"%@/Autos",[paths objectAtIndex:0]] copy];
        } else if (IsCompany == _objType){
            _saveAutosPath = [[NSString stringWithFormat:@"%@/company", [ConfigCodeHelper shareInstance].padDataPath] copy];
        } else {
            _saveAutosPath = [[NSString stringWithFormat:@"%@/video", [ConfigCodeHelper shareInstance].padDataPath] copy];
        }
        
        BOOL isDir;
        if (!([[NSFileManager defaultManager] fileExistsAtPath:_saveAutosPath isDirectory:&isDir] && isDir)) {
            NSError * error = nil;
            [[NSFileManager defaultManager] createDirectoryAtPath:_saveAutosPath
                                      withIntermediateDirectories:YES
                                                       attributes:nil
                                                            error:&error];
        }
        
        self.state = Default_State;
    }
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

-(void) encodeWithCoder:(NSCoder *)aCoder
{
	[aCoder encodeObject:_uuid forKey:@"KEY_UUID"];
    [aCoder encodeObject:_tempCarPath forKey:@"KEY_TempCarPath"];
    [aCoder encodeObject:_tempFilePath forKey:@"KEY_TempFilePath"];
    [aCoder encodeObject:_tempInfoPath forKey:@"KEY_TempInfoPath"];
    [aCoder encodeObject:_saveFilePath forKey:@"KEY_SaveFilePath"];
    [aCoder encodeObject:_saveAutosPath forKey:@"KEY_SaveAutosPath"];
    [aCoder encodeObject:[NSNumber numberWithInteger:_state] forKey:@"KEY_State"];
    [aCoder encodeObject:[NSNumber numberWithFloat:self.progress] forKey:@"KEY_Progress"];
    [aCoder encodeObject:_eTag forKey:@"KEY_ETag"];
    [aCoder encodeObject:[NSNumber numberWithInteger:_objType] forKey:@"KEY_DownloadObjTyp"];
}

-(id) initWithCoder:(NSCoder *)aDecoder
{
	if (self=[super init])
	{
		self.uuid = [aDecoder decodeObjectForKey:@"KEY_UUID"];
        self.tempCarPath = [aDecoder decodeObjectForKey:@"KEY_TempCarPath"];
        self.tempFilePath = [aDecoder decodeObjectForKey:@"KEY_TempFilePath"];
        self.tempInfoPath = [aDecoder decodeObjectForKey:@"KEY_TempInfoPath"];
        self.saveFilePath  = [aDecoder decodeObjectForKey:@"KEY_SaveFilePath"];
        self.saveAutosPath  = [aDecoder decodeObjectForKey:@"KEY_SaveAutosPath"];
        
//        self.state = Pausing_State;
        self.state = [[aDecoder decodeObjectForKey:@"KEY_State"] integerValue];
        self.progress = [[aDecoder decodeObjectForKey:@"KEY_Progress"] floatValue];
        self.eTag = [aDecoder decodeObjectForKey:@"KEY_ETag"];
        self.objType = [[aDecoder decodeObjectForKey:@"KEY_DownloadObjTyp"] integerValue];
	}
	return (self);
}

@end
